/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RollView.java
 *
 * Created on 12.04.2011, 18:27:55
 */
package earthdawnnet.client;

import earthdawnnet.util.DiceCup;
import earthdawnnet.events.client.ClientEvent;
import earthdawnnet.events.client.NewLineEvent;
import earthdawnnet.events.server.RollEvent;
import earthdawnnet.util.Chat;
import javax.swing.DefaultComboBoxModel;
import org.jdesktop.application.Action;

/**
 *
 * @author Andreas Franek
 */
public class RollView extends javax.swing.JFrame
{

    private Client client;
    private Chat chat;
    private User player;

    /** Creates new form RollView */
    public RollView(Client client)
    {
        this.chat = client.getChat();
        this.client = client;
        this.player = User.getInstance();
        setTitle("Choose you destiny...");
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        stepChooser = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        againstField = new javax.swing.JFormattedTextField();

        setName("Form"); // NOI18N
        setResizable(false);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(earthdawnnet.client.EarthdawnNetApp.class).getContext().getResourceMap(RollView.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        stepChooser.setModel(new DefaultComboBoxModel(DiceCup.getDiceDesc()));
        stepChooser.setName("stepChooser"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(earthdawnnet.client.EarthdawnNetApp.class).getContext().getActionMap(RollView.class, this);
        jButton1.setAction(actionMap.get("roll")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        againstField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getIntegerInstance())));
        againstField.setName("againstField"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(stepChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(againstField, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(stepChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(againstField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static RollView startUp(Client client)
    {
        final RollView rf = new RollView(client);
//        java.awt.EventQueue.invokeLater(new Runnable()
//        {
//            public void run()
//            {
//                //rf.setVisible(true);
//            }
//        });
        return rf;
    }

    @Action
    public void roll()
    {
        String namePrefix = player.getName()+": ";
        int step = stepChooser.getSelectedIndex() + 1;
        String against = againstField.getText();
        //System.out.println(against);
        if(against.equalsIgnoreCase(""))
        {

            if(DiceCup.checkStepExists(step))
                client.sendServerEvent(new RollEvent(step));
            else
                chat.add("Error: step does not exist.");
        }
        else
        {
            if(DiceCup.checkStepExists(step))
                client.sendServerEvent(new RollEvent(step, Integer.valueOf(against)));
            else
                chat.add("Error: step does not exist.");
        }
        //System.out.println(evt.getActionCommand());
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField againstField;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JComboBox stepChooser;
    // End of variables declaration//GEN-END:variables
}
